﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThongTinNhanKhau.model.BO
{
    class TienAnTienSuBO
    {
        model.DAO.TienAnTienSuDAO tienAnTienSuDAO = new DAO.TienAnTienSuDAO();
        private GUI.TienAnTienSu f;
        private GUI.NhanKhau nhanKhau;

        public TienAnTienSuBO(GUI.TienAnTienSu tienAnTienSu, GUI.NhanKhau nhanKhau)
        {
            // TODO: Complete member initialization
            this.f = tienAnTienSu;
            this.nhanKhau = nhanKhau;
        }

        internal void loadDefault(string maNhanKhau)
        {
            DataTable dt = new DataTable();
            dt = tienAnTienSuDAO.loadDefault(maNhanKhau);

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    this.f.tbHoTen.Text = dr["HoTen"].ToString();
                    this.f.tbCMND.Text = dr["CMND"].ToString();
                }
            }
        }

        internal void loadTable(string maNhanKhau)
        {
            DataTable dt = tienAnTienSuDAO.loadTable(maNhanKhau);
            if (dt.Rows.Count != 0)
            {
                this.f.gridControlTATS.DataSource = dt;
            }
            else
            {
                this.f.gridControlTATS.DataSource = null;
            }
        }

        internal void genarateMaTATS()
        {
            //genarate MaTamTru
            StringBuilder maTATS = new StringBuilder();
            Random random = new Random();

            do
            {
                maTATS.Clear();
                maTATS.Append("MTATS");
                for (int i = 0; i < 5; i++)
                {
                    maTATS.Append(random.Next(0, 9));
                }
            } while (tienAnTienSuDAO.isValidMaTATS(maTATS.ToString()));

            this.f.tbMaTATS.Text = maTATS.ToString();
        }

        internal void btnLuu()
        {
            String maTATS = this.f.tbMaTATS.Text;
            String maNhanKhau = this.f.tbMaNhanKhau.Text;
            String ngayViPham = this.f.dateTimeNgayViPham.Value.ToString("yyyy-MM-dd");
            String noiDung = this.f.tbNoiDung.Text;
            String hinhThucXuLy = this.f.tbHinhThucXuLy.Text;

            //if Update
            if (tienAnTienSuDAO.isValidMaTATS(maTATS))
            {
                if (tienAnTienSuDAO.capNhatTATS(maTATS, maNhanKhau, ngayViPham, noiDung, hinhThucXuLy))
                {
                    System.Windows.Forms.MessageBox.Show("Cập nhật thành công");
                    loadTable(maNhanKhau);
                    nhanKhau.loadTable();
                }
            }
            else //if New
            {
                if (tienAnTienSuDAO.themMoiTATS(maTATS, maNhanKhau, ngayViPham, noiDung, hinhThucXuLy))
                {
                    System.Windows.Forms.MessageBox.Show("Thêm mới thành công");
                    loadTable(maNhanKhau);
                    nhanKhau.loadTable();
                }
            }
        }

        internal void deleteRow(string maTATS, string maNhanKhau)
        {
            if (tienAnTienSuDAO.deleteRow(maTATS, maNhanKhau))
            {
                System.Windows.Forms.MessageBox.Show("Xóa thành công");
                loadTable(maNhanKhau);
                nhanKhau.loadTable();
            }
        }
    }
}
